import sys, random
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QSystemTrayIcon, QMenu, QAction
from PyQt5.QtCore import QTimer, Qt, QPropertyAnimation, QPoint, QParallelAnimationGroup, QEasingCurve
from PyQt5.QtGui import QIcon
# 🌸 温馨祝福语
messages = [
    "早安 ☀️", "加油 💪", "保持微笑 😊", "你值得被温柔以待 🌷",
    "照顾好自己 ❤️", "记得吃早餐 🍳", "生活要开心 🥰", "努力就会有收获 ✨",
    "今天也要顺顺利利 🍀", "愿你心情阳光明媚 ☀️", "小小幸福满满 🌸",
    "愿烦恼随风而去 🌬️", "勇敢做自己 💖", "偶尔奖励自己 🎁", "多喝水保持健康 💧",
    "每天都是新的开始 🌅", "梦想照进现实 ✨", "保持好奇心 🔎", "微笑面对每一天 😊",
    "心中有爱，眼里有光 💕", "今天也要元气满满 🌟", "温暖的事随处可见 🌻",
    "生活不止眼前的苟且 🍵", "学会享受小确幸 🐾", "保持耐心，一切都会好起来 🌈",
    "感恩每一天 🙏", "快乐其实很简单 😊", "心情好，一切都好 🌸", "愿你有好运常伴 🍀",
    "做个温柔又坚强的人 💖", "每个梦都甜美 💭"
]
# 🌈 温暖颜色
warm_colors = [
    (255, 239, 213), (255, 228, 196), (255, 222, 173), (255, 182, 193), (255, 204, 153),
    (255, 250, 205), (255, 245, 238), (255, 228, 225), (255, 218, 185), (255, 160, 122),
    (255, 192, 203), (255, 182, 255), (255, 240, 245), (250, 235, 215), (255, 228, 181),
    (255, 250, 240), (245, 245, 220), (255, 239, 222), (255, 240, 245), (255, 250, 250),
    (240, 255, 255), (255, 250, 240), (250, 240, 230), (245, 245, 245), (240, 248, 255),
    (255, 105, 180), (255, 140, 0), (255, 69, 0), (255, 99, 71), (255, 127, 80),
    (255, 182, 193), (255, 20, 147), (219, 112, 147), (199, 21, 133), (255, 215, 0),
    (255, 165, 0), (255, 140, 0), (255, 69, 0), (218, 112, 214), (221, 160, 221),
    (238, 130, 238), (255, 0, 255), (255, 20, 147), (255, 105, 180), (255, 182, 193),
    (255, 160, 122), (250, 128, 114), (255, 218, 185), (255, 228, 181), (255, 240, 245)
]
endTime = 27000
outTime = 30000
class Popup(QWidget):
    """单个气泡弹窗，从顶部掉落"""
    def __init__(self, message, color, screen):
        super().__init__()
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Tool)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setFixedSize(200, 50)

        label = QLabel(message, self)
        label.setAlignment(Qt.AlignCenter)
        # 增加边框
        label.setStyleSheet(f"""
            QLabel {{
                font-size: 14px;
                font-family: "微软雅黑";
                color: #333;
                background-color: rgba({color[0]}, {color[1]}, {color[2]}, 240);
                border-radius: 10px;
                border: 2px solid #ccc;
                padding: 6px 12px;
            }}
        """)
        layout = QVBoxLayout(self)
        layout.addWidget(label)
        layout.setContentsMargins(0, 0, 0, 0)

        self.screen = screen
        self.target_x = random.randint(screen.left(), screen.right() - self.width())
        self.target_y = random.randint(screen.top(), screen.bottom() - self.height())

        start_pos = QPoint(self.target_x, screen.top() - self.height())
        self.setGeometry(start_pos.x(), start_pos.y(), self.width(), self.height())
        self.show()
        self.animate_appearance(start_pos)

    def animate_appearance(self, start_pos):
        anim = QPropertyAnimation(self, b"pos")
        anim.setDuration(300)  # 弹窗掉落动画
        anim.setStartValue(start_pos)
        anim.setEndValue(QPoint(self.target_x, self.target_y))
        anim.setEasingCurve(QEasingCurve.OutCubic)
        anim.start()
        self.anim = anim  # 防止被回收
class PopupGenerator:
    """弹窗生成器"""
    def __init__(self, app):
        self.app = app
        self.screen = QApplication.primaryScreen().availableGeometry()
        self.activePopups = []
        self.animations = []
        self.close_animation_group = None
        self.running = True

        self.timer = QTimer()
        self.timer.timeout.connect(self.show_popup)
        self.timer.start(50)  # 每50毫秒生成一个弹窗

    def show_popup(self):
        if self.running:
            popup = Popup(random.choice(messages), random.choice(warm_colors), self.screen)
            self.activePopups.append(popup)

    def stop_generating(self):
        self.running = False

    def fly_all_to_bottom_center(self):
        """关闭动画：滑行到底部中间"""
        target_x = self.screen.left() + self.screen.width() // 2 - 100
        target_y = self.screen.bottom() - 50

        group = QParallelAnimationGroup()
        self.close_animation_group = group

        for popup in self.activePopups:
            anim = QPropertyAnimation(popup, b"pos")
            anim.setDuration(600)
            anim.setStartValue(popup.pos())  # 从当前位置开始滑行
            anim.setEndValue(QPoint(target_x, target_y))
            anim.setEasingCurve(QEasingCurve.InQuad)
            anim.finished.connect(popup.close)
            self.animations.append(anim)
            group.addAnimation(anim)

        group.finished.connect(self.app.quit)
        group.start()
class SystemTrayApp:
    """托盘应用"""
    def __init__(self, app):
        self.app = app
        self.tray_icon = QSystemTrayIcon(QIcon(), parent=app)
        self.tray_icon.setToolTip("Popup Controller")
        self.menu = QMenu()
        exit_action = QAction("退出", self.menu)
        exit_action.triggered.connect(self.app.quit)
        self.menu.addAction(exit_action)
        self.tray_icon.setContextMenu(self.menu)
        self.tray_icon.show()
if __name__ == "__main__":
    app = QApplication(sys.argv)
    generator = PopupGenerator(app)
    tray_app = SystemTrayApp(app)
    QTimer.singleShot(endTime, generator.stop_generating)
    QTimer.singleShot(outTime, generator.fly_all_to_bottom_center)
    sys.exit(app.exec_())
